//
//  NSObject+GGJson.m
//  ocdemo
//
//  Created by JIENING ZHANG on 2019/6/11.
//  Copyright © 2019 JIENING ZHANG. All rights reserved.
//

#import "NSObject+GGJson.h"

@implementation NSObject (GGJson)

- (NSString *)ggJsonStringPrettty {
    
    if( [NSJSONSerialization isValidJSONObject:self] ) {
        
        NSError *err;
        
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:&err];
        //NSLog(@"jsonData length: %lu", [jsonData length]);
    
        if(jsonData.length > 0) {
            NSString* jsonString =  [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
            //NSLog(@"jsonString : %@", jsonString);
            
            return jsonString;
        } else {
            //NSLog(@"dataWithJSONObject error : %@", err);
        }
    } else {
        //NSLog(@"NSJSONSerialization isValidJSONObject, return no");
    }
    
    return nil;
}

- (NSString *)ggJsonString {
    if( [NSJSONSerialization isValidJSONObject:self] ) {
        
        NSError *err;
        
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self options:0 error:&err];
        //NSLog(@"jsonData length: %lu", [jsonData length]);
        
        if(jsonData.length > 0) {
            NSString* jsonString =  [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
            //NSLog(@"jsonString : %@", jsonString);
            
            return jsonString;
        }
    }
    
    return nil;
}

@end
